package com.jpyl.jpMedicalTreatment.view.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.google.gson.JsonObject;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.DateOfCheckModel;
import com.jpyl.jpMedicalTreatment.model.ErrorMessageModel;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.jpyl.jpMedicalTreatment.view.adapter.DateOfCheckAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 病人首页-检查日期
 * Created by 15697 on 2019/2/27.
 */

public class DateOfCheckActivity extends BaseActivity {

    @BindView(R.id.rv_refresh_layout)
    RecyclerView rvRefreshLayout;
    @BindView(R.id.mr_refresh_layout)
    MaterialRefreshLayout mrRefreshLayout;
    @BindView(R.id.tv_refreshLayout_Empty)
    TextView tvRefreshLayoutEmpty;

    DateOfCheckAdapter adapter;
    String title;
    final RequestApi publicApi = Network.getRequestApi();
    List<DateOfCheckModel.RowsBean> list = new ArrayList<>();
    int page = 1, listSize = 0, pos;
    String userid = "";
    RxSpf_UserModel spfUserModel;
    long dateFrom = 0;
    long dateTo = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_refrsh_layout;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        mTitleTv.setText(R.string.select_dateOfCheck);
        title = getIntent().getStringExtra("title");
        pos = getIntent().getIntExtra("pos", -1);
        adapter = new DateOfCheckAdapter(this, list);
        rvRefreshLayout.setAdapter(adapter);
        rvRefreshLayout.setLayoutManager(new GridLayoutManager(this, 1));
        mrRefreshLayout.setLoadMore(true);
        mrRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                //刷新
                page = 1;
                getList(pos);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                super.onRefreshLoadMore(materialRefreshLayout);
                //加载更多
                if (10 == listSize) {
                    ++page;
                    getList(pos);
                } else {
                    materialRefreshLayout.finishRefreshLoadMore();
                }
            }
        });
        adapter.setItemClickListener(new DateOfCheckAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getApplicationContext(), CheckDetailsActivity.class)
                        .putExtra("title", title).putExtra("list", list.get(position)).putExtra
                                ("pos", pos));
            }
        });

        spfUserModel = RxSpf_UserModel.create(this);
        userid = spfUserModel.userId().get();

        getList(pos);
    }

    private void getList(int pos) {
        switch (pos) {
            case 0:
                jpylGetHPV();
                break;
            case 1:
                jpylGetHP();
                break;
            case 2:
                jpylGetSe();
                break;
        }
    }

    /**
     * hpv列表
     */
    private void jpylGetHPV() {
        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", page + "");
        publicApi.jpylGetHPV(userid, body.toString(), dateFrom, dateTo).map(new Func1<ResponseModel<DateOfCheckModel>, DateOfCheckModel>() {
            @Override
            public DateOfCheckModel call(ResponseModel<DateOfCheckModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DateOfCheckModel>() {
                    @Override
                    public void onCompleted() {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        }
                    }

                    @Override
                    public void onNext(DateOfCheckModel userModel) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                        listSize = userModel.getTotal();
                        if (page == 1) {
                            list.clear();
                        }
                        list.addAll(userModel.getRows());
                        adapter.notifyDataSetChanged();

                    }
                });
    }

    /**
     * hp列表
     */
    private void jpylGetHP() {
        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", page + "");

        publicApi.jpylGetHP(userid, body.toString(), dateFrom, dateTo).map(new Func1<ResponseModel<DateOfCheckModel>, DateOfCheckModel>() {
            @Override
            public DateOfCheckModel call(ResponseModel<DateOfCheckModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DateOfCheckModel>() {
                    @Override
                    public void onCompleted() {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        }
                    }

                    @Override
                    public void onNext(DateOfCheckModel userModel) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                        listSize = userModel.getTotal();
                        if (page == 1) {
                            list.clear();
                        }
                        list.addAll(userModel.getRows());
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    /**
     * se列表
     */
    private void jpylGetSe() {
        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", page + "");

        publicApi.jpylGetSe(userid, body.toString(), dateFrom, dateTo).map(new Func1<ResponseModel<DateOfCheckModel>, DateOfCheckModel>() {
            @Override
            public DateOfCheckModel call(ResponseModel<DateOfCheckModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DateOfCheckModel>() {
                    @Override
                    public void onCompleted() {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();

                    }

                    @Override
                    public void onError(Throwable e) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();

                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        }
                    }

                    @Override
                    public void onNext(DateOfCheckModel userModel) {
                        mrRefreshLayout.finishRefreshLoadMore();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshing();
                        listSize = userModel.getTotal();
                        if (page == 1) {
                            list.clear();
                        }
                        list.addAll(userModel.getRows());
                        adapter.notifyDataSetChanged();
                    }
                });
    }


}
